package com.exartisan.thanhdoanhn;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.exartisan.adapter.ContestListAdapter;
import com.exartisan.adapter.EventListAdapter;
import com.exartisan.adapter.PostListAdapter;
import com.exartisan.bean.Event;
import com.exartisan.bean.Post;
import com.exartisan.bean.Survey;
import com.exartisan.network.NetworkUtility;
import com.exartisan.network.RestConnector;
import com.exartisan.network.StupidResponseTranslater;
import com.exartisan.network.WeirdParametersBuilder;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;


public class ListAcivity extends BaseActivity {
	
	private ListView list;
	private int type;
	private int postType;
	
	private EventListAdapter evAdapter;
	private PostListAdapter postAdapter;
	private ContestListAdapter contestAdapter;
	
	ArrayList<Event> events;
	ArrayList<Post> posts;
	ArrayList<Survey> surveys;
	ProgressDialog progressDialog;
	
	@Override
	protected void setUpActionBar() {
		super.setUpActionBar();
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setCustomView(R.layout.ab_list);
	}

	@Override
	protected void initUI() {
		setContentView(R.layout.activity_list);
		list = (ListView) findViewById(R.id.listView);
	}

	@Override
	protected void initProcess() {
		TextView tvTitle = (TextView)findViewById(R.id.tvTitle);
		type = getIntent().getIntExtra("type", 0);
		postType = getIntent().getIntExtra("postType", 1);
		
		switch (type) {
		case 0:
			tvTitle.setText("Sự kiện");
			doNetWorkTask(WeirdParametersBuilder.BuildParams(WeirdParametersBuilder.BuildEventData()));
			break;
		case 1:
			tvTitle.setText("Bài viết");
			int id = (int)getIntent().getLongExtra("categoryId", 0);
			doNetWorkTask(WeirdParametersBuilder.BuildParams(WeirdParametersBuilder.BuildPostData(id, postType)));
			break;
		case 2:
			tvTitle.setText("Cuộc thi");
			doNetWorkTask(WeirdParametersBuilder.BuildParams(WeirdParametersBuilder.BuildSurveyData(1)));
			break;
		case 3:
			doNetWorkTask((WeirdParametersBuilder.BuildParams(WeirdParametersBuilder.BuildCategoryForumData())));
			break;

		default:
			break;
		}
		
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				Intent i = new Intent();
				switch (type) {
				case 0:
					i = new Intent(getBaseContext(), EventFeedActivity.class);
					event = events.get(pos);
					break;
				case 1:
					i = new Intent(getBaseContext(), ForumDetailActivity.class);
					post = posts.get(pos);
					break;
				case 2:
					PackageManager pm = getPackageManager();
					i = pm.getLaunchIntentForPackage("com.exartisan.molisasurvey");
					break;
				case 3:
					i = new Intent(getBaseContext(), ListActivity.class);
					i.putExtra("type", 1);
					i.putExtra("categoryId", Long.valueOf(4));
					break;
				default:
					break;
				}
				startActivity(i);
			}
		});
	}
	
	private void doNetWorkTask(RequestParams requestParams){
		if(NetworkUtility.checkNetworkState(getApplicationContext())){
			progressDialog = ProgressDialog.show(getActivity(), "Đang kết nối", "Đang tải dữ liệu");
			RestConnector.post("",requestParams, new AsyncHttpResponseHandler(){
				public void onSuccess(int arg0, String response) {
					progressDialog.dismiss();
					switch (type) {
					case 0:
						events = StupidResponseTranslater.getEvent(response);
						evAdapter = new EventListAdapter(getBaseContext(), events);
						list.setAdapter(evAdapter);
						break;
					case 1:
						posts = StupidResponseTranslater.getPosts(response);
						if (posts.size() < 1) {
							Toast.makeText(getBaseContext(), "Không có dữ liệu", Toast.LENGTH_SHORT).show();
							finish();
						}
						postAdapter = new PostListAdapter(getBaseContext(), posts);
						list.setAdapter(postAdapter);
						break;
					case 2:
						surveys = StupidResponseTranslater.getSurvey(response);
						contestAdapter = new ContestListAdapter(getBaseContext(), surveys);
						list.setAdapter(contestAdapter);
						break;

					default:
						break;
					}
					super.onSuccess(arg0, response);
				}
				
				@Override
				public void onFailure(Throwable arg0, String arg1) {
					Toast.makeText(getActivity(), "Không có dữ liệu", Toast.LENGTH_SHORT).show();
					finish();
					super.onFailure(arg0, arg1);
				}
			});
		} else {
			Toast.makeText(getBaseContext(), "Vui lòng bật kết nối Internet", Toast.LENGTH_SHORT).show();
			finish();
		}
	}

	public void onClickBack(View v){
		finish();
	}

}
